package com.eternal.widget.guqiang;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * Created by Administrator
 * On 2020/5/4
 * Description:
 * @author Administrator
 */
public class DoubleCloseAddLayout extends LinearLayout {
    private IEffectBar one, two;
    private Listener listener;
    public DoubleCloseAddLayout(Context context) {
        this(context, null);
    }

    public DoubleCloseAddLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DoubleCloseAddLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public DoubleCloseAddLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        post(new Runnable() {
            @Override
            public void run() {
                init();
            }
        });
    }

    private void init(){
        one= (IEffectBar) getChildAt(0);
        two= (IEffectBar) getChildAt(2);
        initOne();
        initTow();
    }

    private void initOne(){
        one.setListener(new IEffectBar.Listener() {
            @Override
            public void onDown(boolean isProgress) {
                if(listener!=null){
                    listener.onFirstDown(isProgress);
                }
            }

            @Override
            public void onChange(int value) {
                if(one.isChecked()){
                    two.setProgress(one.getTx(), false);
                }
                if(listener!=null){
                    listener.onFirst(value);
                }
            }

            @Override
            public void onUp(boolean isProgress, int value) {
                if(listener!=null){
                    listener.onEndFirst(isProgress,value);
                }
            }

            @Override
            public void onChecked(boolean b) {
                if(b){
                    if(two.isChecked()){
                        one.setProgress(two.getTx(), true);
                    }
                    two.setProgress(one.getTx(), false);
                }else{
                    two.setProgress(0, false);
                }
                if(listener!=null){
                    listener.onFirstChecked(b);
                }
            }
        });
    }

    private void initTow(){
        two.setListener(new IEffectBar.Listener() {
            @Override
            public void onDown(boolean isProgress) {
                if(listener!=null){
                    listener.onLastDown(isProgress);
                }
            }

            @Override
            public void onChange(int value) {
                if(two.isChecked()){
                    one.setProgress(two.getTx(), true);
                }
                if(listener!=null){
                    listener.onLast(value);
                }
            }

            @Override
            public void onUp(boolean isProgress, int value) {
                if(listener!=null){
                    listener.onEndLast(isProgress,value);
                }
            }

            @Override
            public void onChecked(boolean b) {
                if(b){
                    if(one.isChecked()){
                        two.setProgress(one.getTx(), false);
                    }
                    one.setProgress(two.getTx(), true);
                }else{
                    one.setProgress(-1, true);
                }
                if(listener!=null){
                    listener.onLastChecked(b);
                }
            }
        });
    }

    public void setListener(Listener listener){
        this.listener=listener;
    }

    public void setFactory(final IEffectBar.Factory factory){
        post(new Runnable() {
            @Override
            public void run() {
                one.setFactory(factory);
                two.setFactory(factory);
            }
        });
    }

    public interface Listener{
        void onFirstDown(boolean isButton);
        void onLastDown(boolean isButton);
        void onFirst(int value);
        void onLast(int value);
        void onEndFirst(boolean isButton,int value);
        void onEndLast(boolean isButton, int value);
        void onFirstChecked(boolean b);
        void onLastChecked(boolean b);
    }
}
